//
//  UIView+MZLayout.m
//  MZAnimationDemo
//
//  Created by Chris Yang on 2016/11/24.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "UIView+MZLayout.h"

@implementation UIView(MZLayout)

#pragma mark - Get X

- (CGFloat)mzGetXForSubviewToLeftWithSpace:(CGFloat)space {
    return self.bounds.origin.x + space;
}

- (CGFloat)mzGetXForSubviewToRightWithSpace:(CGFloat)space viewWidth:(CGFloat)viewWidth {
    return self.bounds.origin.x + self.bounds.size.width - space - viewWidth;
}

- (CGFloat)mzGetXForSubviewInCentenWithOffset:(CGFloat)offset viewWidth:(CGFloat)viewWidth {
    return self.bounds.origin.x + (self.bounds.size.width - viewWidth) / 2 + offset;
}

/* Brother view */
- (CGFloat)mzGetXForBrotherViewToLeftWithSpace:(CGFloat)space {
    return self.frame.origin.x + self.frame.size.width + space;
}

- (CGFloat)mzGetXForBrotherViewToRightWithSpace:(CGFloat)space viewWidth:(CGFloat)viewWidth {
    return self.frame.origin.x - space - viewWidth;
}

- (CGFloat)mzGetXForBrotherViewInCentenWithOffset:(CGFloat)offset viewWidth:(CGFloat)viewWidth {
    return self.frame.origin.x + (self.frame.size.width - viewWidth) / 2 + offset;
}

#pragma mark - Get Y

- (CGFloat)mzGetYForSubviewToTopWithSpace:(CGFloat)space {
    return self.bounds.origin.y + space;
}

- (CGFloat)mzGetYForSubviewToBottomWithSpace:(CGFloat)space viewHeight:(CGFloat)viewHeight {
    return self.bounds.origin.y + self.bounds.size.height - space - viewHeight;
}

- (CGFloat)mzGetYForSubviewInCentenWithOffset:(CGFloat)offset viewHeight:(CGFloat)viewHeight {
    return self.bounds.origin.y + (self.bounds.size.height - viewHeight) / 2 + offset;
}

/* Brother view */
- (CGFloat)mzGetYForBrotherViewToTopWithSpace:(CGFloat)space {
    return self.frame.origin.y +self.frame.size.height + space;
}

- (CGFloat)mzGetYForBrotherViewToBottomWithSpace:(CGFloat)space viewHeight:(CGFloat)viewHeight {
    return self.frame.origin.y - space - viewHeight;
}

- (CGFloat)mzGetYForBrotherViewInCentenWithOffset:(CGFloat)offset viewHeight:(CGFloat)viewHeight {
    return self.frame.origin.y + (self.frame.size.height - viewHeight) / 2 + offset;
}

#pragma mark - Rect Single

- (CGRect)mzRectInSubviewWithWidth:(CGFloat)width
                          height:(CGFloat)height
                          toLeft:(CGFloat)toLeft
                           toTop:(CGFloat)toTop {
    
    return CGRectMake([self mzGetXForSubviewToLeftWithSpace:toLeft],
                      [self mzGetYForSubviewToTopWithSpace:toTop],
                      width,
                      height);
}

- (CGRect)mzRectInSubviewWithWidth:(CGFloat)width
                          height:(CGFloat)height
                         toRight:(CGFloat)toRight
                           toTop:(CGFloat)toTop {
    
    return CGRectMake([self mzGetXForSubviewToRightWithSpace:toRight viewWidth:width],
                      [self mzGetYForSubviewToTopWithSpace:toTop],
                      width,
                      height);
}

- (CGRect)mzRectInSubviewWithWidth:(CGFloat)width
                          height:(CGFloat)height
                          toLeft:(CGFloat)toLeft
                        toBottom:(CGFloat)toBottom {
    
    return CGRectMake([self mzGetXForSubviewToLeftWithSpace:toLeft],
                      [self mzGetYForSubviewToBottomWithSpace:toBottom viewHeight:height],
                      width,
                      height);
}

- (CGRect)mzRectInSubviewWithWidth:(CGFloat)width
                          height:(CGFloat)height
                         toRight:(CGFloat)toRight
                        toBottom:(CGFloat)toBottom {
    
    return CGRectMake([self mzGetXForSubviewToRightWithSpace:toRight viewWidth:width],
                      [self mzGetYForSubviewToBottomWithSpace:toBottom viewHeight:height],
                      width,
                      height);
}

#pragma mark - Rect Center

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height {
    return [self mzCenterRectInSubviewWithWidth:width height:height xOffset:0 yOffser:0];
}

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height
                               xOffset:(CGFloat)xOffset
                               yOffser:(CGFloat)yOffset {
    
    return CGRectMake([self mzGetXForSubviewInCentenWithOffset:xOffset viewWidth:width],
                      [self mzGetYForSubviewInCentenWithOffset:yOffset viewHeight:height],
                      width,
                      height);
}

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height
                                toLeft:(CGFloat)toLeft {
    
    return CGRectMake([self mzGetXForSubviewToLeftWithSpace:toLeft],
                      [self mzGetYForSubviewInCentenWithOffset:0 viewHeight:height],
                      width,
                      height);
}

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height
                               toRight:(CGFloat)toRight {
    
    return CGRectMake([self mzGetXForSubviewToRightWithSpace:toRight viewWidth:width],
                      [self mzGetYForSubviewInCentenWithOffset:0 viewHeight:height],
                      width,
                      height);
}

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height
                                 toTop:(CGFloat)toTop {
    
    return CGRectMake([self mzGetXForSubviewInCentenWithOffset:0 viewWidth:width],
                      [self mzGetYForSubviewToTopWithSpace:toTop],
                      width,
                      height);
}

- (CGRect)mzCenterRectInSubviewWithWidth:(CGFloat)width
                                height:(CGFloat)height
                              toBottom:(CGFloat)toBottom {
    
    return CGRectMake([self mzGetXForSubviewInCentenWithOffset:0 viewWidth:width],
                      [self mzGetYForSubviewToBottomWithSpace:toBottom viewHeight:height],
                      width,
                      height);
}

#pragma mark -

- (CGRect)mzCenterRectInSubviewWithView:(UIView *)childView {
    
    return [self mzCenterRectInSubviewWithWidth:childView.bounds.size.width
                                       height:childView.bounds.size.height];
}

- (CGRect)mzCenterRectInSubviewWithView:(UIView *)childView toLeft:(CGFloat)toLeft {
    
    return [self mzCenterRectInSubviewWithWidth:childView.bounds.size.width
                                       height:childView.bounds.size.height
                                       toLeft:toLeft];
}

- (CGRect)mzCenterRectInSubviewWithView:(UIView *)childView toRight:(CGFloat)toRight {
    
    return [self mzCenterRectInSubviewWithWidth:childView.bounds.size.width
                                       height:childView.bounds.size.height
                                      toRight:toRight];
}

- (CGRect)mzCenterRectInSubviewWithView:(UIView *)childView toTop:(CGFloat)toTop {
    
    return [self mzCenterRectInSubviewWithWidth:childView.bounds.size.width
                                       height:childView.bounds.size.height
                                        toTop:toTop];
}

- (CGRect)mzCenterRectInSubviewWithView:(UIView *)childView toBottom:(CGFloat)toBottom {
    
    return [self mzCenterRectInSubviewWithWidth:childView.bounds.size.width
                                       height:childView.bounds.size.height
                                     toBottom:toBottom];
}

@end
